//
//  MethodTool.m
//  Learn
//
//  Created by zyl on 2024/4/3.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "MethodTool.h"
#import "UpdateAppAlertView.h"
@implementation MethodTool
+ (double)getTextHeightWithStr:(NSString *)str font:(UIFont *)font ViewWidth:(double)width
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size.height + 1;
}
+ (double)getTextWidthWithStr:(NSString *)str font:(UIFont *)font ViewHeight:(double)height
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size.width + 1;
}
+ (NSString *)dateToStyle1WithDate:(double)date;//yyyy-MM-dd hh:mm
{
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    
    NSString * dateString = [dateFormatter stringFromDate:date1];
    return dateString;
}
+ (void)callWithMobileNumber:(NSString *)mobile
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",mobile]]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",mobile]]];
    }
    
}
+ (void)getCookieWithwebView:(WKWebView *)web success:(void(^)(BOOL success))successBlock
{
    //网页加载完成取出 cookies

    WKHTTPCookieStore *store = web.configuration.websiteDataStore.httpCookieStore;
    [store getAllCookies:^(NSArray<NSHTTPCookie *> * _Nonnull cookie) {
       
        NSHTTPCookie *cooki= cookie.lastObject;
        if ([cooki value]) {
            lApp.cookie = [NSString stringWithFormat:@"%@=%@",[cooki name],[cooki value]];
        }
        successBlock(YES);
    }];
}
+ (double)getAudioMp3TimeWithUrl:(NSString *)url
{
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:url] options:nil];
    return CMTimeGetSeconds(asset.duration);
}
+ (void)getAudioMp3TimeWithUrl:(NSString *)url complete:(void(^)(double time))compBlock
{
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:url] options:nil];
    compBlock(CMTimeGetSeconds(asset.duration));
}
+ (NSString *)getPlayTimeWithTime:(int )time
{
    int m = time/60;
    int s = time%60;
    
    return [NSString stringWithFormat:@"%02d:%02d",m,s];
    
}
+ (void)goWebviewWithUrl:(NSString *)url
{
    UIViewController *reciteVC = [[UIViewController alloc] initWithClassName:@"SafariViewController"];
    reciteVC.paraObject = url;
    [(BaseNavigationController *)lApp.window.rootViewController pushViewController:reciteVC animated:YES];
}
+ (void)checkMicrophonePromiss:(BaseViewController *)vc
{
    void(^StartSpeakingVoice)(void) = ^() {
        
      
    };
    
    //检测是否授权麦克风
    void(^CheckOutAuthorization)(void) = ^() {
        AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        if (videoAuthStatus == AVAuthorizationStatusNotDetermined) {// 未询问用户是否授权
            //第一次询问用户是否进行授权
            [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
                if (granted) {
                    StartSpeakingVoice();
                }
            }];
        }
        else if(videoAuthStatus == AVAuthorizationStatusRestricted || videoAuthStatus == AVAuthorizationStatusDenied) {// 未授权
            NSString *message = @"\n麦克风权限未开启，请进入系统【设置】>【隐私】>【麦克风】中打开开关,开启麦克风功能";
            [vc alertViewTitle:@"麦克风权限未开启" message:message left:@"确定" leftBlock:^{
                if (vc.navigationController.viewControllers.count > 1)[vc.navigationController popViewControllerAnimated:YES];
            } right:@"前往" rightBlock:^{
                //跳入当前App设置界面
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }];
        }
        else{// 已授权
         
        }
    };
    
    CheckOutAuthorization();
}
+ (void)checkVersionUpdateWithInfo:(NSDictionary *)info
{
    if (!info) return;
    UIViewController *vc = lApp.window.rootViewController;
    UpdateAppAlertView *alertView = [[UpdateAppAlertView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, lScreenHeight)];
    alertView.dic = info;
    [vc.view addSubview:alertView];
    
}
+(NSString *)appVersion
{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

@end
